# -*- coding: iso-8859-1 -*-
# file Game.py
# author Rocco Martino
#
############################################################################
#    Copyright (C) 2015 by Rocco Martino                                   #
#    martinorocco@gmail.com                                                #
#                                                                          #
#    This program is free software; you can redistribute it and/or modify  #
#    it under the terms of the GNU Lesser General Public License as        #
#    published by the Free Software Foundation; either version 2.1 of the  #
#    License, or (at your option) any later version.                       #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU Lesser General Public License for more details.                   #
#                                                                          #
#    You should have received a copy of the GNU Lesser General Public      #
#    License along with this program; if not, write to the                 #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

############################################################################
from . import ODECallback, Sensor, Controller, Actuator
############################################################################




############################################################################
# ........................................................................ #
# ........................................................................ #
############################################################################




############################################################################
# ........................................................................ #
class Game(ODECallback.ODECallback):





############################################################################
############################################################################









############################################################################
    def __init__(self, obj, data):
        super(Game, self).__init__(obj, data)

        self.ClassName = "ooGame::Game"
        self.Name = "Game@" + obj.name
############################################################################




############################################################################
    def writeData(self, writer):
        super(Game, self).writeData(writer)


        state = 0

        for i in range ( len(self.BlenderObject.game.states_initial) ):
            state += int(self.BlenderObject.game.states_initial[i]) << i


        writer.writeLine("State %d" % state)






        sensor_list = self.BlenderObject.game.sensors

        controller_list = self.BlenderObject.game.controllers

        actuator_list = self.BlenderObject.game.actuators






        if sensor_list:

            writer.moveIn("SensorList %d" % len(sensor_list))

            for sensor in sensor_list:

                if sensor.type == "ALWAYS":
                    Sensor.AlwaysSensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "COLLISION":
                    Sensor.CollisionSensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "MOUSE":
                    Sensor.MouseSensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "PROPERTY":
                    Sensor.PropertySensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "NEAR":
                    Sensor.NearSensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "DELAY":
                    Sensor.DelaySensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "RANDOM":
                    Sensor.RandomSensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "KEYBOARD":
                    Sensor.KeyboardSensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                elif sensor.type == "MESSAGE":
                    Sensor.MessageSensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)

                else:
                    print("OOE unsupported sensor: %s@%s, %s" % (sensor.name, self.BlenderObject.name, sensor.type))
                    Sensor.Sensor( self.BlenderObject, self.Data, sensor ).writeObject(writer)



            writer.moveOut("SensorList %d" % len(sensor_list))



        if controller_list:

            writer.moveIn("ControllerList %d" % len(controller_list))

            for controller in controller_list:

                if "LOGIC_" in controller.type:
                    Controller.Controller(self.BlenderObject, self.Data, controller).writeObject(writer)

                else:
                    print("OOE unsupported controller: %s@%s, %s" % ( controller.name, self.BlenderObject.name, controller.type ))
                    Controller.Controller( self.BlenderObject, self.Data, controller ).writeObject(writer)



            writer.moveOut("ControllerList %d" % len(controller_list))



        if actuator_list:

            writer.moveIn("ActuatorList %d" % len(actuator_list))

            for actuator in actuator_list:


                if actuator.type == "VISIBILITY":
                    Actuator.VisibilityActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "STATE":
                    Actuator.StateActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "MOTION":
                    Actuator.MotionActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "PROPERTY":
                    Actuator.PropertyActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "SCENE":
                    Actuator.SceneActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "EDIT_OBJECT":
                    Actuator.EditObjectActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "CAMERA":
                    Actuator.CameraActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "MESSAGE":
                    Actuator.MessageActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "GAME":
                    Actuator.GameActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                elif actuator.type == "MOUSE":
                    Actuator.MouseActuator(self.BlenderObject, self.Data, actuator).writeObject(writer)


                else:
                    print("OOE unsupported actuator: %s@%s, %s" % ( actuator.name, self.BlenderObject.name, actuator.type ))
                    Actuator.Actuator( self.BlenderObject, self.Data, actuator ).writeObject(writer)



            writer.moveOut("ActuatorList %d" % len(actuator_list))
############################################################################




# ........................................................................ #
############################################################################
